import Vue from "vue";
import VueRouter from "vue-router";
import routes from './routes';

Vue.use(VueRouter);

const router = new VueRouter({
	mode: "hash",           // hash 加#       history   /
	base: process.env.BASE_URL,
	routes
});
// 解决ElementUI导航栏中的vue-router在3.0版本以上重复点菜单报错问题
const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
	return originalPush.call(this, location).catch(err => err)
}
//  路由守卫配置
router.beforeEach((to, from, next) => {
	const token = localStorage.getItem('token') ? JSON.parse(localStorage.getItem('store')).token : '';
	let {name} = to || {};
	const noTokenCheck = ['login'];
	if (name == 'login') {
		localStorage.removeItem('store');
	}
	if (token || noTokenCheck.includes(name)) {
		next()
	} else {
		next('/login')
	}
});
router.afterEach(() => {
});

export default router;
